var form ;
$(function(){
    layui.use(['layer', 'form'], function() {
        form = layui.form();
        init()
    });
})
function init()
{
    loadDict();
    getArea(1, 1);

    form.on('checkbox(isPedigreeFilter)', function (data) {
        if (data.elem.checked) {
            $("#isPedigree").val(data.value);
            $("#photoBox").show();
            $("#yearBox").show();
        } else {
            $("#photoBox").hide();
            $("#yearBox").hide();
            $("#isPedigree").val("0");
        }
    });
    form.on('checkbox(isRegister)', function (data) {
        if (data.elem.checked) {
            $("#isRegister").val(data.value)
        } else {
            $("#isRegister").val("0")
        }
    });
    form.on('checkbox(isVoice)', function (data) {
        if (data.elem.checked) {
            $("#isVoice").val(data.value)
        } else {
            $("#isVoice").val("0")
        }
    });
    $('#btn').bind('click', function () {
        var form = $("form").serialize();
        console.log(form)
        if (checkStrLength($("#password").val(), 6, 16) == false) {
            return false;
        } else if(!check()){
            return false;
        } else {
            submitCommit();
        }
    });
    $('#password').bind('change', function () {
        checkStrLength($("#password").val(), 6, 16);
    });
    $('#buyerName').bind('change', function () {
        chinese($("#buyerName").val());
    });
}
function loadDict(){
    $.ajax({
        url:server_url+"/rest/dictionary/getDicNameValue.do?typeName=scale",
        data:{ran:Math.random()},
        success:function(data){
            var scale = data.data;
            var html = ' <option value="">请选择规模</option>'
            for (var i = 0; i< scale.length ; i++){
                html += ' <option value="'+scale[i].dicValue +'">'+scale[i].dicName+'</option>'
            }
            $("#scale1").html(html);
            form.render("select");
            form.on('select(scale)', function(data){
                var pro = data.value;
                $("#scale").val(pro);
            });
        }
    });
    $.ajax({
        url:server_url+"/rest/dictionary/getDicNameValue.do?typeName=avgYield",
        data:{ran:Math.random()},
        success:function(data){
            var avgField = data.data;
            var html = ' <option value="">请选择平均单产</option>'
            for (var i = 0; i< avgField.length ; i++){
                html += ' <option value="'+avgField[i].dicValue +'">'+avgField[i].dicName+'</option>'
            }
            $("#avgYield1").html(html)
            form.render("select");
            form.on('select(avgYield)', function(data){
                var pro = data.value;
                $("#avgYield").val(pro);
            });
        }
    });
    $.ajax({
        url:server_url+"/rest/dictionary/getDicNameValue.do?typeName=pedigreeYear",
        data:{ran:Math.random()},
        success:function(data){
            var pedigreeYear = data.data;
            var html = ' <option value="">请选择谱系年份</option>'
            for (var i = 0; i< pedigreeYear.length ; i++){
                html += ' <option value="'+pedigreeYear[i].dicValue +'">'+pedigreeYear[i].dicName+'</option>'
            }
            $("#pedigreeYear1").html(html)
            form.render("select");
            form.on('select(pedigreeYear)', function(data){
                var pro = data.value;
                $("#pedigreeYear").val(pro);
            });
        }
    });
}
function check(){
    if($("#supplyName").val() == ''){
        easyAlert("请填写供应商名称");
        return false;
    }
    if($("#mobile").val() == ''){
        easyAlert("请填写供应商联系方式");
        return false;
    }
    if($("#compname").val() == ''){
        easyAlert("请填写供应商公司名称");
        return false;
    }
    if($("#weixin").val() == '')
    {
        easyAlert("请填写供应商微信号");
        return false;
    }
    if($('#consProvince').val() != null &&$('#consProvince').val() != ''){
    }else{
        alert('请选择收货省');return;
    }
    if($('#consCity').val() != null && $('#consCity').val() != ''){
    }else{
        alert('请选择收货市');return;
    }
    if($('#consCounty').val() != null &&$('#consCounty').val() != ''){
    }else{
        alert('请选择收货县');return;
    }
    if($('#addr').val() != null && $('#addr').val()!= ''){
    }else{
        alert('请填写详细地址');return;
    }
    var scale = $("#scale").val();
    if( scale== null || scale == ''){
        easyAlert("请选择规模")
        return false;
    }
    var avgYield = $("#avgYield").val();
    if(avgYield == null || avgYield == '')
    {
        easyAlert("请选择平均单产")
        return false;
    }
    var bankCard = $("#bankCard").val();
    if(bankCard == null || bankCard == '')
    {
        easyAlert("请填写银行卡号")
        return false;
    }
    var bank = $("#bank").val();
    if(bank == null || bank == '')
    {
        easyAlert("请填写开户行")
        return false;
    }
    var supplyName = $("#supplyName").val();
    if(supplyName == null || supplyName == '')
    {
        easyAlert("请填写开户名")
        return false;
    }
    var isPedigree = $("#isPedigree").val()
    if( isPedigree == '1')
    {
        var pedigreeYear = $("#pedigreeYear").val()
        if(pedigreeYear == null || pedigreeYear == '')
        {
            easyAlert("请选择谱系年份")
            return false;
        }
        var pedigreeImg= $("#pedigreeImg").val()
        if(pedigreeImg == null || pedigreeImg == '')
        {
            easyAlert("请上传谱系照片")
            return false;
        }
    }

    return true;
}
function checkStrLength(userPass, min, max) {// 验证密码
    if(userPass == null || userPass == ''){
        easyAlert("请输入密码!");
    }
    if (userPass.length < min || userPass.length > max || userPass == "") {
        easyAlert("密码长度须在" + min + "-" + max + "之间!");
        return false;
    } else {
        return  true;
    }
}
function chinese(china) { // 验证只能是中文
    var pattern = /^[\u4e00-\u9fa5]*$/;
    if (!pattern.test(china) || china == "") {
        easyAlert("名称不能为空且必须为中文");
        return false;
    } else {
        return  true;
    }
}
function submitCommit() {
    $.ajax({
        type : 'post',
        async: false,
        dataType : 'json',
        url : server_url + '/rest/supply/register/register.do?ran='+Math.random()+"&staffid="+userid,
        data : $("form").serializeArray(),
        contentType : "application/x-www-form-urlencoded; charset=utf-8",
        success : function(data) {
            if (data.retCode == "0000") {
                countDown(3);
            } else {
                easyAlert("信息有误，请重新输入");
            }
        }
    });
}

var getArea = function(parent,servel){
    $.ajax({
        url:server_url+'rest/areaManager/getByParent.do?parentId='+parent,
        data:{ran:Math.random()},
        type:'get',
        error:function(){alert(contants.systemError);},
        success:function(msg){
            //console.log(msg);
            var areas = msg.data;
            var s = '';
            for(var i=0;i<areas.length;i++){
                s +='<option value="'+areas[i].areaId+'">'+areas[i].areaName+'</option>';
            }
            if(servel ==1){
                s= '<option value="">请选择省</option>' +s;
                $("#consProvince1").html(s);
                form.render("select");
                form.on('select(consProvince)', function(data){
                    var pro = data.value;
                    $("#consProvince").val(pro);
                    getArea(pro,2);
                });

            }else if(servel == 2){
                s= '<option value="">请选择市</option>' +s;
                $("#consCity1").html(s);
                form.render("select");
                form.on('select(consCity)', function(data){
                    $("#consCity").val(data.value);
                    console.log($('#consCity').val())
                    getArea(data.value,3)
                });
            }else if(servel == 3){
                s= '<option value="">请选择县</option>' +s;
                $("#consCounty1").html(s);
                form.render("select");
                form.on('select(consCounty)', function(data){
                    console.log(data.value)
                    var pro = data.value;
                    $("#consCounty").val(pro);
                });
            }

        }

    })
}
function countDown(secconds){
    easyAlert("添加成功");
    window.location.href="/LianniuWeb/staff/clients.do";
}